package com.magictower.primary.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.format.Formatter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.magictower.primary.R;
import com.magictower.primary.bean.AppInfo;

import java.util.List;

public class AppManagerAdapter extends BaseAdapter {

    private List<AppInfo> userAppInfos;
    private List<AppInfo> systemAppInfos;
    private Context mContext;

    public AppManagerAdapter(Context context, List<AppInfo> userAppInfos, List<AppInfo> systemAppInfos) {
        this.mContext = context;
        this.userAppInfos = userAppInfos;
        this.systemAppInfos = systemAppInfos;
    }

    @Override
    public int getCount() {
        return userAppInfos.size() + 1 + systemAppInfos.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        if (position == 0) {
            return null;
        } else if (position == userAppInfos.size() + 1) {
            return null;
        }
        AppInfo appInfo;
        if (position < userAppInfos.size() + 1) {
            //把多出来的特殊的条目减掉
            appInfo = userAppInfos.get(position - 1);
        } else {
            int location = userAppInfos.size() + 2;
            appInfo = systemAppInfos.get(position - location);
        }
        return appInfo;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //如果当前的position等于0 表示应用程序
        if (position == 0) {
            TextView textView = new TextView(mContext);
            textView.setTextColor(Color.WHITE);
            textView.setBackgroundColor(Color.GRAY);
            textView.setText("用户程序(" + userAppInfos.size() + ")个");
            return textView;
            //表示系统程序
        } else if (position == userAppInfos.size() + 1) {
            TextView textView = new TextView(mContext);
            textView.setTextColor(Color.WHITE);
            textView.setBackgroundColor(Color.GRAY);
            textView.setText("系统程序(" + systemAppInfos.size() + ")个");
            return textView;
        }

        AppInfo appInfo;
        if (position < userAppInfos.size() + 1) {
            //把多出来的特殊的条目减掉
            appInfo = userAppInfos.get(position - 1);
        } else {
            int location = userAppInfos.size() + 2;
            appInfo = systemAppInfos.get(position - location);
        }

        View view;
        ViewHolder holder;
        if (convertView != null && convertView instanceof LinearLayout) {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        } else {
            view = View.inflate(mContext, R.layout.item_app_manager, null);
            holder = new ViewHolder();
            holder.iv_icon = (ImageView) view.findViewById(R.id.iv_icon);
            holder.tv_apk_size = (TextView) view.findViewById(R.id.tv_apk_size);
            holder.tv_location = (TextView) view.findViewById(R.id.tv_location);
            holder.tv_name = (TextView) view.findViewById(R.id.tv_name);
            view.setTag(holder);
        }

        holder.iv_icon.setImageDrawable(appInfo.getIcon());
        holder.tv_apk_size.setText(Formatter.formatFileSize(mContext, appInfo.getApkSize()));
        holder.tv_name.setText(appInfo.getApkName());

        if (appInfo.isRom()) {
            holder.tv_location.setText("手机内存");
        } else {
            holder.tv_location.setText("外部存储");
        }

        return view;
    }

    private static class ViewHolder {
        ImageView iv_icon;
        TextView tv_apk_size;
        TextView tv_location;
        TextView tv_name;
    }
}